package sealte.wdgf.control.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.sql.DataSource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import sealte.wdgf.control.bean.PetBean;
import sealte.wdgf.control.servlet.system.SystemGetServlet;
import sealte.wdgf.model.pojo.Pet;
import sealte.wdgf.model.system.SessionMapObject;

public class HtmlContentTransferServlet extends SystemGetServlet {

	private static final long serialVersionUID = 7681906763539198590L;
	static Logger logger = LoggerFactory.getLogger(HtmlContentTransferServlet.class);
	
	@Override
	protected void doBusinessService(HttpServletRequest req,
			HttpServletResponse resp) throws ServletException, IOException {
		ServletContext application = req.getServletContext();
		DataSource ds = (DataSource)application.getAttribute("DataSource");
		Connection conn = null;
		try {
			conn = ds.getConnection();
		} catch (SQLException e) {
			logger.error("获取Connection实例时出错！原因："+e.getMessage());
		}
		PetBean pb = new PetBean(conn);
		List<Pet> petList = pb.getAll();
		try {
			if(null!=conn){
				conn.close();
			}
		} catch (SQLException e) {
			logger.error("执行Connection-close操作时出错！原因："+e.getMessage());
		}
		HttpSession session = req.getSession();
		session.setAttribute("petList", petList);
		String sessionId = session.getId();
		SessionMapObject smo = (SessionMapObject)application.getAttribute("SessionMap");
		//修改该sessionId对应的id
		smo.remove(sessionId);
		smo.add(sessionId, req.getRemoteHost()+":"+req.getRemotePort());
		application.setAttribute("SessionMap", smo);
		req.getRequestDispatcher("admin/").forward(req, resp);
		super.doBusinessService(req, resp);
	}
	
	
	

}
